package com.pandora.credit.service.impl;

import com.pandora.common.util.Constants;
import com.pandora.credit.dao.ShareDao;
import com.pandora.credit.dto.response.ShareResponse;
import com.pandora.credit.entity.Share;
import com.pandora.credit.exception.InvalidResponseException;
import com.pandora.credit.mongo.entity.MongoShare;
import com.pandora.credit.mongo.service.MongoShareService;
import com.pandora.credit.service.ShareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author developer
 */
@Service
public class ShareServiceImpl implements ShareService {
    @Autowired
    private ShareDao shareDao;
    @Autowired
    private MongoShareService mongoShareService;

    @Override
    public List<Share> getByPageValid(long pageId, int valid) {
        return shareDao.selectByPageValid(pageId, valid);
    }

    @Override
    public ShareResponse doGetShare(long pageId) {
        List<Share> shares = getByPageValid(pageId, Constants.ITEM_STATUS_VALID);
        if(shares == null || shares.size() <= 0){
            throw new InvalidResponseException();
        }
        Share share = shares.get(0);
        MongoShare mongoShare = mongoShareService.queryById(share.getShareId());

        return new ShareResponse(
                Constants.RETURN_TYPE_SHARING,
                mongoShare.getSharing(),
                String.valueOf(pageId)
        );
    }
}
